﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel.Web;
using System.Json;
using System.IO;
using System.Runtime.Serialization;

namespace VMS_SL
{
    public partial class MainPage : UserControl
    {
        // JsonArray chargecode;
        List<string> ObjLst = new List<string>();

        string HostURI = "http://localhost:4895/MastersService.svc/Masters/";

        public MainPage()
        {
            InitializeComponent();

            BindChargeCodes();
            BindMasterTypes();
            BindCustomUnitCodes();

        }

        private void BindChargeCodes()
        {
            string URI = HostURI + "charge_codes/";
            WebClient client = new WebClient();
            Uri uri = new Uri(URI);
            client.OpenReadCompleted += client_OpenReadCompleted;
            client.OpenReadAsync(uri);


        }

        private void BindMasterTypes()
        {
            string URI = HostURI + "master_types/";
            WebClient client = new WebClient();
            Uri uri = new Uri(URI);
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompletedForMasterTypes);
            client.OpenReadAsync(uri);

        }

        private void BindCustomUnitCodes()
        {
            string URI = HostURI + "cust_unit_codes/";
            WebClient client = new WebClient();
            Uri uri = new Uri(URI);
            client.OpenReadCompleted += client_OpenReadCompletedForUnitCodes;
            client.OpenReadAsync(uri);
             
            

        }

        private void BindMasterItems(string Item_Type)
        {
            string URI = HostURI + Item_Type + "/";
            WebClient client = new WebClient();
            Uri uri = new Uri(URI);
            client.OpenReadCompleted += client_OpenReadCompletedForMasterItems;
            client.OpenReadAsync(uri);

        }

        private void BindUnitCodeGrid()
        {

            string URI = HostURI + "unit_codes/";
            WebClient client = new WebClient();
            Uri uri = new Uri(URI);
            client.OpenReadCompleted += client_OpenReadCompletedForDefaultUnitCodes;
            client.OpenReadAsync(uri);

        }

        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                JsonArray JSonItems = (JsonArray)JsonArray.Load(e.Result);
                //if (ObjLst == null)
                //{
                ObjLst = new List<string>();
                //}
                foreach (JsonPrimitive item in JSonItems)
                {

                    ObjLst.Add(item);

                }
                cmbchargecode.ItemsSource = ObjLst;

            }


        }

        void client_OpenReadCompletedForMasterTypes(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                JsonArray JSonItems = (JsonArray)JsonArray.Load(e.Result);
                //if (ObjLst == null)
                //{
                ObjLst = new List<string>();
                //}
                foreach (JsonPrimitive item in JSonItems)
                {

                    ObjLst.Add(item);

                }
                cmbmastertypes.ItemsSource = ObjLst;

            }


        }

        void client_OpenReadCompletedForUnitCodes(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                JsonArray JSonItems = (JsonArray)JsonArray.Load(e.Result);
                //if (ObjLst == null)
                //{
                ObjLst = new List<string>();
                //}
                foreach (JsonPrimitive item in JSonItems)
                {

                    ObjLst.Add(item);

                }
                cmbcustomunitcode.ItemsSource = ObjLst;

            }


        }

        void client_OpenReadCompletedForDefaultUnitCodes(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                JsonArray JSonItems = (JsonArray)JsonArray.Load(e.Result);
                //if (ObjLst == null)
                //{
                ObjLst = new List<string>();
                //}
                foreach (JsonPrimitive item in JSonItems)
                {

                    ObjLst.Add(item);
                   

                }
                

            }


        }

        void client_OpenReadCompletedForMasterItems(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                JsonArray JSonItems = (JsonArray)JsonArray.Load(e.Result);
                //if (ObjLst == null)
                //{
                ObjLst = new List<string>();
                //}
                foreach (JsonPrimitive item in JSonItems)
                {

                    ObjLst.Add(item);

                }

                cmbmasteritems.ItemsSource = ObjLst;
            }

        }

        private void masteritems_selectionchanged(object sender, SelectionChangedEventArgs e)
        {
            BindMasterItems(cmbmastertypes.SelectedValue.ToString());

        }

        private void btnadditemparameter_Click(object sender, RoutedEventArgs e)
        {
            if (cmbmasteritems.SelectedValue != null)
            {
                lstrateparameters.Items.Add(cmbmasteritems.SelectedValue);
            }
            else
            {
                //todo logging and exception handling
            }
         }

        private void btnremoveitemparameters_Click(object sender, RoutedEventArgs e)
        {
            lstrateparameters.Items.RemoveAt(lstrateparameters.SelectedIndex);
        }
    }
}

